import java.io.File;
import java.io.IOException;

import javax.swing.text.Document;
import javax.swing.text.AsyncBoxView.ChildLocator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.soap.Node;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderAdapter;

//  http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
public class Parser {
//	Graph graph;
//	public Parser(Graph graph){
//		this.graph = graph;
//	}
//	
	public boolean parse(Graph graph){
		try{
			File inputfile = new File("/home/govinda/uni/NP/projekt/besp.xml");
			DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFac.newDocumentBuilder();
			org.w3c.dom.Document doc = dBuilder.parse(inputfile);
			doc.getDocumentElement().normalize();
			
			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			NodeList nodeList = doc.getElementsByTagName("node");
			NodeList edgeList = doc.getElementsByTagName("edge");
			for (int tmp = 0; tmp < nodeList.getLength(); tmp++) {
				org.w3c.dom.Node node = nodeList.item(tmp);
				NodeList childList = node.getChildNodes();
				int id = 0;
				int cap = 0;
				
//				id = getTagValue("id", childList.item(0));
//				for (int tmp2 = 0; tmp2 < childList.getLength(); tmp2++) {
//					org.w3c.dom.Node nnode = childList.item(tmp2);
//					if (nnode.getNodeName().contains("id")) {
//						id = Integer.valueOf(nnode.getNodeValue());
////						id = Integer.parseInt(nnode.getNodeValue());   !!execption
////						graph.addKn(new Knoten(Integer.parseInt(nnode.getNodeValue())));
//					}
//					if (nnode.getNodeName().contains("capacity")) {
//						cap = Integer.valueOf(nnode.getNodeValue());
//					}
//
//				}
					graph.addKn(new Knoten(id, cap));
			} //ende knoten
			
			for (int tmp = 0; tmp < edgeList.getLength(); tmp++){
				org.w3c.dom.Node edge = edgeList.item(tmp);
				NodeList childList = edge.getChildNodes();
				int srcid = 0;
				int dstid = 0;
				
//				for (int tmp2 = 0; tmp2 < childList.getLength(); tmp2++) {
//					org.w3c.dom.Node nnode = childList.item(tmp2);
//					
//					if (nnode.getNodeName().contains("srcid")) {
//						srcid = Integer.valueOf(nnode.getNodeValue());
////						graph.addKn(new Knoten(Integer.parseInt(nnode.getNodeValue())));
//					}
//					if (nnode.getNodeName().contains("dstid")) {
//						dstid = Integer.valueOf(nnode.getNodeValue());
//					}
//
//				}
				graph.getKn(dstid).addPre(graph.getKn(srcid));
			}
			
			
		}catch (Exception e){
			 e.printStackTrace();

		System.err.println("parser exception");
		return false;
		/*dumdidum*/}
		return true;
	}
	
	private static String getTagValue(String sTag, Element eElement){
	    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	    Node nValue = (Node) nlList.item(0); 
	 
	    return nValue.getNodeValue();    
	 }
}
